package com.app.controller;

import cn.hutool.core.util.StrUtil;
import com.app.model.AdminUserModel;
import com.app.model.DepartmentModel;
import com.app.model.FileClassModel;
import com.app.service.AdminUserService;
import com.app.service.FileClassService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@Controller
@RequestMapping("/api")
public class FileController {
    @Autowired
    private FileClassService fileClassService;

    @Autowired
    private AdminUserService adminUserService;

    /**
     * 获取训练教材分类
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getFileTypeList")
    public Result<Object> getTextList(Integer pid, String keyword,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        List<FileClassModel> modelList = fileClassService.getModelList(pid,keyword);
        return Result.success(findChildren(modelList, pid));
    }

    private List<FileClassModel> findChildren(List<FileClassModel> modelList, Integer pid) {
        List<FileClassModel> fileClassModelList = new ArrayList<>();
        for (int i = 0; i < modelList.size(); i++) {
            if (Objects.equals(modelList.get(i).getPid(), pid)) {
                FileClassModel model = modelList.get(i);
                List<FileClassModel> modelList1 = findChildren(modelList, model.getId());
                for (int j = 0; j < modelList1.size(); j++) {
                    model.setChild(modelList1);
                }
                fileClassModelList.add(model);
            }
        }
        return fileClassModelList;
    }

    /**
     * 插入文件类型
     *
     * @param fileClassModel 文件类模型
     * @param request        请求
     * @return {@link Result}<{@link Object}>
     */
    @PostMapping("/insertFileType")
    public Result<Object> insertFileType(@RequestBody FileClassModel fileClassModel, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (fileClassModel.getId() != null && fileClassModel.getId() > 0) {
            FileClassModel model = fileClassService.getModel(fileClassModel.getId());
            if (model != null) {
                model.setName(fileClassModel.getName());
                model.setSort(fileClassModel.getSort());
                if (fileClassModel.getPid() != null) {
                    model.setPid(fileClassModel.getPid());
                }
                fileClassService.updateById(model);
            }
        } else {
            fileClassModel.setStatus(1);
            fileClassService.save(fileClassModel);
        }
        return Result.success("操作成功");
    }

    /**
     * 删除文件类型
     *
     * @param id      id
     * @param request 请求
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/deleteFileType")
    public Result<Object> deleteFileType(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        FileClassModel classModel = fileClassService.getById(id);
        if (classModel == null) {
            return Result.error(500, "该分类已不存在");
        }
        classModel.setStatus(2);
        fileClassService.updateById(classModel);
        return Result.success("操作成功");
    }

    /**
     * 详细信息文件类型
     *
     * @param id      id
     * @param request 请求
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/detailFileType")
    public Result<Object> detailFileType(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        FileClassModel classModel = fileClassService.getById(id);
        if (classModel == null) {
            return Result.error(500, "该分类已不存在");
        }
        return Result.success(classModel);
    }

    /**
     * 状态文件类型
     *
     * @param id      id
     * @param status  状态
     * @param request 请求
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/statusFileType")
    public Result<Object> statusFileType(Integer id, Integer status, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        FileClassModel classModel = fileClassService.getById(id);
        if (classModel == null) {
            return Result.error(500, "该分类已不存在");
        }
        classModel.setStatus(status);
        fileClassService.updateById(classModel);
        return Result.success(classModel);
    }
}
